﻿using System.Collections;
using System.Collections.Generic;
using Pathfinding;
using UnityEngine;

public class MonsterController : MonoBehaviour
{
    GameBoard gameBoard;

    float speed = 2.0f;
    float speedFactor = 1.0f;
    private AIPath aiPath;
    public Animator animator;
    public MonsterAttackSensor attackSensor;
    float attackGap = 0;
    float setSlimeGap = 3f;
    int hp = 100;
    bool burn = false;
    float burnGap = 1f;
    
    public bool canAttack = true;

    public void GetHurt(int hurt) {
        Debug.Log("Get Hurt");
        hp -= hurt;
        Debug.Log(hp);
        if (hp <= 0) {
            animator.SetTrigger("Die");
        }
    }

    private void updateGap() {       
    }

    private void Awake()
    {
        gameBoard = GameObject.Find("Board").GetComponent<GameBoard>();
        aiPath = gameObject.GetComponent<AIPath>();        
        animator = gameObject.GetComponent<Animator>();
        attackSensor = gameObject.GetComponentInChildren<MonsterAttackSensor>();
        canAttack = true;
    }

    // Start is called before the first frame update
    void Start()
    {
        aiPath.endReachedDistance = 0.5f + Random.value;
        aiPath.maxSpeed = speed * speedFactor;
    }

    // Update is called once per frame
    void Update()
    {
        if (burnGap > 0) {
            burnGap -= Time.deltaTime;
        }
        if (attackGap > 0)
        {
            attackGap -= Time.deltaTime;
        }
        if (setSlimeGap > 0) {
            setSlimeGap -= Time.deltaTime;
        }
        Vector3 vec = aiPath.desiredVelocity;
        animator.SetFloat("Mag", vec.x * vec.x +vec.y * vec.y);
        if (vec.x >= 0)
        {
            animator.SetFloat("X", 1f);
        }
        else
        {
            animator.SetFloat("X", 0f);
        }
        if (attackSensor.targetNum > 0 && attackGap <= 0 && canAttack) {
            animator.SetTrigger("Attack");
            attackGap = 3f;
        }
        if (burn && burnGap <= 0) {
            GetHurt(10);
            burnGap = 1f;
        }
        if (setSlimeGap < 0) {
            gameBoard.setSlime(transform.localPosition);
            setSlimeGap = 3f;
        }
    }

    public void setSpeed(float speed) {
        this.speed = speed;
        aiPath.maxSpeed = speed * speedFactor;
    }

    private void OnAttackEnter() {
        canAttack = false;
        aiPath.canMove = false;
    }

    private void OnAttackExit() {
        canAttack = true;
        aiPath.canMove = true;
        attackSensor.hurtTargetTable(20);
    }

    private void OnDieEnter()
    {
        canAttack = false;
        aiPath.canMove = false;
    }

    private void OnDieExit()
    {
        gameBoard.setSlime(transform.localPosition);
        Destroy(gameObject);
    }

    private void EnterTree(bool burn) {
        this.burn = burn;
        aiPath.maxSpeed = 0.4f * speed;
    }

    private void ExitTree() {        
        burn = false;
        aiPath.maxSpeed = speed;
    }

    private void EnterWater() {
        aiPath.maxSpeed = 0.4f * speed;
    }
    

    private void ExitWater() {
        aiPath.maxSpeed = speed;
    }
}
